/* Copyright (c) 2011 Vitria Technology, Inc.  All rights reserved. */
package com.vitria.m3oui.rest.app.mgr;

import java.io.FileInputStream;

import java.io.IOException;

import org.junit.Test;
import org.restlet.data.Form;
import org.restlet.representation.Representation;
import org.restlet.resource.ClientResource;
import com.qilin.hammer.util.BaseRestService;

public class AppsTest extends BaseRestService {
	public static String APP_ID = "/sla";
    public static String APP_URL_PREFIX = URL_PREFIX + "/app.mgr/apps";
    
    public static String INSTALL_UID = "AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAASLA";
    public static String UPGRADE_UID = "BBBBBBBB-AAAA-AAAA-AAAA-AAAAAAAAASLA";
    public static String EXPORT_UID =  "CCCCCCCC-AAAA-AAAA-AAAA-AAAAAAAAASLA";
    
    public static String kpi1ContentFile = "D:/work/main/yoda/m3o/ui/devtests/junit/app.memory/resources/kpi1Content.txt";
    //used for KPI model
    private String kpiUrlBase = URL_PREFIX + "/app/sla/user/vtbaadmin/kpi/SLA";
	private String saveOp = "?op=save";
	private String deployOp = "?op=deploy";
	private String startOp = "?op=start";
	private String stopOp = "?op=stop";
	private String deleteOp = "?op=delete";
	public AppsTest(String name) {
        super(name);
    }
	
//	@Test
//	public void testInstallWorkbenchApp() throws IOException{
//      String url = APP_URL_PREFIX +"workbenchapp?op=install&uid=WWWWWW";
//      ClientResource resource = createSecureClientResource(url);
//      //resource.addQueryParameter("uid", "sample.zip");
//      Form form = new Form();
//		//form.add("op", "install");
//		//form.add("uid", INSTALL_UID);
//	  form.add("title", "<#&$!@@'\"中文中文sla sla\"'!$&#>");
//	  form.add("description", "<desc>This is #description# for app \"sla\".&&&>>> 这是%app% 'sla'的描述</desc>");
//	  form.add("version","1.1");
//	  Representation rep = resource.post(form.getWebRepresentation());
//      String text = rep.getText();
//      System.out.println("testInstallApp:");
//      System.out.println(text);
//      assertNotNull(text);
//	}
//    
//    @Test
//    public void testInstallApp() throws IOException{
//        String appID = APP_ID;
//        String url = APP_URL_PREFIX + appID+"?op=install&uid="+INSTALL_UID;
//        ClientResource resource = createSecureClientResource(url);
//        //resource.addQueryParameter("uid", "sample.zip");
//        Form form = new Form();
//		//form.add("op", "install");
//		//form.add("uid", INSTALL_UID);
//		form.add("title", "<#&$!@@'\"中文中文sla sla\"'!$&#>");
//		form.add("description", "<desc>This is #description# for app \"sla\".&&&>>> 这是%app% 'sla'的描述</desc>");
//		form.add("version","1.1");
//		Representation rep = resource.post(form.getWebRepresentation());
//        String text = rep.getText();
//        System.out.println("testInstallApp:");
//        System.out.println(text);
//        assertNotNull(text);
//    }
    
//    
//  @Test
//  public void testInstallAppWithNoPermission() throws IOException{
//	  //Need change another user who doesn't have admin role to get a token in this case
//      String appID = APP_ID;
//      String url = APP_URL_PREFIX + appID+"?op=install&uid="+INSTALL_UID;
//      ClientResource resource = createSecureClientResource(url);
//      Form form = new Form();
//	  Representation rep = resource.post(form.getWebRepresentation());
//      String text = rep.getText();
//      System.out.println("testInstallApp:");
//      System.out.println(text);
//      assertNotNull(text);
//  }
	
//    @Test
//    public void testInstallAppInvalidVer() throws IOException{
//        String url = APP_URL_PREFIX + "/invalidVer?op=install&uid=legel_version";
//        ClientResource resource = createSecureClientResource(url);
//        //resource.addQueryParameter("uid", "sample.zip");
//        Form form = new Form();
//		//form.add("op", "install");
//		//form.add("uid", INSTALL_UID);
//		Representation rep = resource.post(form.getWebRepresentation());
//        String text = rep.getText();
//        System.out.println("testInstallApp:");
//        System.out.println(text);
//        assertNotNull(text);
//    }
//    
//  @Test
//  public void testInstallUnMatch() throws IOException{
//      String url = APP_URL_PREFIX+"/unMatch?op=install&uid="+INSTALL_UID;
//      ClientResource resource = createSecureClientResource(url);
//      Form form = new Form();
//		//form.add("op", "install");
//		//form.add("uid", INSTALL_UID);
//		//form.add("title", "<#&$!@@'\"中文中文sla sla\"'!$&#>");
//		//form.add("description", "<desc>This is #description# for app \"sla\".&&&>>> 这是%app% 'sla'的描述</desc>");
//		//form.add("version","1.1");
//	  Representation rep = resource.post(form.getWebRepresentation());
//      String text = rep.getText();
//      System.out.println("testInstallApp:");
//      System.out.println(text);
//      assertNotNull(text);
//  }
//    
//    @Test
//    public void testInstallInvalid() throws IOException{
//        String url = APP_URL_PREFIX+"/invalid?op=install&uid="+"IIIIII";
//        ClientResource resource = createSecureClientResource(url);
//        //resource.addQueryParameter("uid", "sample.zip");
//        Form form = new Form();
//		//form.add("op", "install");
//		//form.add("uid", INSTALL_UID);
//		//form.add("title", "<#&$!@@'\"中文中文sla sla\"'!$&#>");
//		//form.add("description", "<desc>This is #description# for app \"sla\".&&&>>> 这是%app% 'sla'的描述</desc>");
//		//form.add("version","1.1");
//		Representation rep = resource.post(form.getWebRepresentation());
//        String text = rep.getText();
//        System.out.println("testInstallApp:");
//        System.out.println(text);
//        assertNotNull(text);
//    }
//    
//    @Test
//    public void testInstallAppSelfDep() throws IOException{
//        String url = APP_URL_PREFIX + "/selfDep?op=install&uid=app_selfdepend";
//        ClientResource resource = createSecureClientResource(url);
//        //resource.addQueryParameter("uid", "sample.zip");
//        Form form = new Form();
//		//form.add("op", "install");
//		//form.add("uid", INSTALL_UID);
//		Representation rep = resource.post(form.getWebRepresentation());
//        String text = rep.getText();
//        System.out.println("testInstallApp:");
//        System.out.println(text);
//        assertNotNull(text);
//    }
//    
//    @Test
//    public void testInstallAppDep() throws IOException{
//        String url = APP_URL_PREFIX + "/Dep?op=install&uid=app_depend";
//        ClientResource resource = createSecureClientResource(url);
//        //resource.addQueryParameter("uid", "sample.zip");
//        Form form = new Form();
//		//form.add("op", "install");
//		//form.add("uid", INSTALL_UID);
//		Representation rep = resource.post(form.getWebRepresentation());
//        String text = rep.getText();
//        System.out.println("testInstallApp:");
//        System.out.println(text);
//        assertNotNull(text);
//    }
    
//    @Test
//    public void testListApp() throws IOException{
//        ClientResource resource = createSecureClientResource(APP_URL_PREFIX);
//        Representation rep = resource.get();
//        String text = rep.getText();
//        System.out.println("testListApp:");
//        System.out.println(text);
//        assertNotNull(text);
//    }
//    
//    @Test
//    public void testEnableApp() throws IOException{
//        String appID = APP_ID;
//        String url = APP_URL_PREFIX + appID + "?op=enable" ;
//        ClientResource resource = createSecureClientResource(url);
//        Representation rep = resource.post(null);
//        String text = rep.getText();
//        System.out.println("testEnableApp:");
//        System.out.println(text);
//        assertNotNull(text);
//    }
//    
//    @Test
//    public void testKPIModel() throws IOException{
//    	
//   	 //KPI model test 
//    	String index = "1";
//    	String opString = kpiUrlBase + index + saveOp;
//    	ClientResource resource = createSecureClientResource(opString);
//
//    	String document = readFromFile(kpi1ContentFile);
//		String properties = "<properties><property name='tags' value=''/></properties>";
//    	
//    	Form form = new Form();
//        form.add("_RESOURCE_Type", "kpi");
//        form.add("document", document);
//        form.add("properties", properties);
//        form.add("space", "private");
//    	
//        // create KPI model SLA{$i}
//    	opString = kpiUrlBase + index + saveOp;
//    	resource = createSecureClientResource(opString);
//    	Representation rep = resource.post(form.getWebRepresentation());
//    	String text = rep.getText();
//    	System.out.print("create KPI"+ index +" model... ");
//    	
////    	// deploy KPI model
////    	opString = kpiUrlBase + index + deployOp;
////    	resource = createSecureClientResource(opString);
////    	rep = resource.get();
////    	text = rep.getText();
////    	System.out.print("deploy KPI"+ index +" model... ");
////    	
//    	// deploy and start KPI model
//    	opString = kpiUrlBase + index  + startOp;
//    	resource = createSecureClientResource(opString);
//    	rep = resource.get();
//    	text = rep.getText();
//    	System.out.print("deploy and start KPI"+ index +" model... ");
//    	
//    	// stop KPI model
//    	opString = kpiUrlBase + index  + stopOp;
//    	resource = createSecureClientResource(opString);
//    	rep = resource.get();
//    	text = rep.getText();
//    	System.out.print("stop KPI"+ index +" model... ");
//    	
//    	// delete KPI model
//    	opString = kpiUrlBase + index  + deleteOp;
//    	resource = createSecureClientResource(opString);
//    	rep = resource.get();
//    	text = rep.getText();
//    	System.out.print("delete KPI"+ index +" model... ");
//    	
//    }
//    @Test
//    public void testInstallProjAsApp() throws IOException{
//        String url = APP_URL_PREFIX +"/prjApp?op=install.prjs&projects=projectuuid:WorkflowTestDB.Project.zip&version=1.0";
//        ClientResource resource = createSecureClientResource(url);
//        //resource.addQueryParameter("uid", "sample.zip");
//        Form form = new Form();
//		//form.add("op", "install");
//		//form.add("uid", INSTALL_UID);
//		form.add("title", "<#&$!@@'\"中文中文\"'!$&#>");
//		form.add("description", "<desc>This is #description# for app \"project\".&&&>>> 这是%app% 'project'的描述</desc>");
//		Representation rep = resource.post(form.getWebRepresentation());
//        String text = rep.getText();
//        System.out.println("testInstallPrjAsApp:");
//        System.out.println(text);
//        assertNotNull(text);
//    }
//    @Test
//    public void testUpgradeAppInvalidVer() throws IOException{
//        String url = APP_URL_PREFIX +"/sample?op=upgrade&uid=invalidverupg";
//        ClientResource resource = createSecureClientResource(url);
//        //resource.addQueryParameter("uid", "sample.zip");
//        Form form = new Form();
//		//form.add("op", "install");
//		//form.add("uid", INSTALL_UID);
//		//form.add("title", "<#&$!@@'\"中文中文\"'!$&#>");
//		//form.add("description", "<desc>This is #description# for app \"project\".&&&>>> 这是%app% 'project'的描述</desc>");
//		Representation rep = resource.post(form.getWebRepresentation());
//        String text = rep.getText();
//        System.out.println("testInstallPrjAsApp:");
//        System.out.println(text);
//        assertNotNull(text);
//    }
//    @Test
//    public void testDisableApp() throws IOException{
//        String appID = APP_ID;
//        String url = APP_URL_PREFIX + appID + "?op=disable" ;
//        ClientResource resource = createSecureClientResource(url);
//        Representation rep = resource.post(null);
//        String text = rep.getText();
//        System.out.println("testDisableApp:");
//        System.out.println(text);
//        assertNotNull(text);
//    } 
//    
//    @Test
//    public void testExportApp() throws IOException{
//        String appID = APP_ID;
//        String url = APP_URL_PREFIX + appID + "?op=export" + "&uid=" + EXPORT_UID ;
//        ClientResource resource = createSecureClientResource(url);
//        Representation rep = resource.post(null);
//        String text = rep.getText();
//        System.out.println("testExportApp:");
//        System.out.println(text);
//        assertNotNull(text);
//    }
//    @Test
//    public void testUpgradeApp() throws IOException{
//    	//http://localhost:8080/m3oui/rest/app.mgr/apps/samplep1_10?op=upgrade&uid=87C9DAB7-CED5-9E57-73D0-674839E3C3DD
//        String appID = APP_ID;
//        String url = APP_URL_PREFIX + appID + "?op=upgrade" + "&uid=" + UPGRADE_UID +"&version=1.2.2";
//        ClientResource resource = createSecureClientResource(url);
//        //resource.addQueryParameter("uid", "sample.zip");
//        Form form = new Form();
//		//form.add("op", "install");
//		//form.add("uid", INSTALL_UID);
//		form.add("title", "<?#&$!@@'\"中文中文sla sla\"'!$&#?>New");
//		//form.add("description", "!#$^'\"<>?中文中文New");
//		//form.add("version","1.2.2");
//		Representation rep = resource.post(form.getWebRepresentation());
//        //Representation rep = resource.post(null);
//        String text = rep.getText();
//        System.out.println("testUpgradeApp:");
//        System.out.println(text);
//        assertNotNull(text);  
//    }
//    
//    @Test
//    public void testKPIModelAfterUpgrade() throws IOException{
//    	
//   	 //KPI model test 
//    	String index = "2";
//    	String opString = kpiUrlBase + index + saveOp;
//    	ClientResource resource = createSecureClientResource(opString);
//
//    	String document = readFromFile(kpi1ContentFile);
//		String properties = "<properties><property name='tags' value=''/></properties>";
//    	
//    	Form form = new Form();
//        form.add("_RESOURCE_Type", "kpi");
//        form.add("document", document);
//        form.add("properties", properties);
//        form.add("space", "private");
//    	
//        // create KPI model SLA{$i}
//    	opString = kpiUrlBase + index + saveOp;
//    	resource = createSecureClientResource(opString);
//    	Representation rep = resource.post(form.getWebRepresentation());
//    	String text = rep.getText();
//    	System.out.print("create KPI"+ index +" model... ");
//    	
////    	// deploy KPI model
////    	opString = kpiUrlBase + index + deployOp;
////    	resource = createSecureClientResource(opString);
////    	rep = resource.get();
////    	text = rep.getText();
////    	System.out.print("deploy KPI"+ index +" model... ");
////    	
//    	// deploy and start KPI model
//    	opString = kpiUrlBase + index  + startOp;
//    	resource = createSecureClientResource(opString);
//    	rep = resource.get();
//    	text = rep.getText();
//    	System.out.print("deploy and start KPI"+ index +" model... ");
//    	
//    	// stop KPI model
//    	opString = kpiUrlBase + index  + stopOp;
//    	resource = createSecureClientResource(opString);
//    	rep = resource.get();
//    	text = rep.getText();
//    	System.out.print("stop KPI"+ index +" model... ");
//    	
//    	// delete KPI model
//    	opString = kpiUrlBase + index  + deleteOp;
//    	resource = createSecureClientResource(opString);
//    	rep = resource.get();
//    	text = rep.getText();
//    	System.out.print("delete KPI"+ index +" model... ");
//    	
//    }
//    @Test
//    public void testUninstallApp() throws IOException{
//        String appID = APP_ID;
//        String url = APP_URL_PREFIX + appID + "?op=uninstall" + "&keepAppConf=false" + "&keepUserData=false" ;
//        ClientResource resource = createSecureClientResource(url);
//        Representation rep = resource.get();
//        String text = rep.getText();
//        System.out.println("testUninstallApp:");
//        System.out.println(text);
//        assertNotNull(text);
//    }
    
//    @Test
//    public void testParameter() throws IOException{
//        String appID = APP_ID;
//        String url = APP_URL_PREFIX + appID+"?op=save.parameters";
//        ClientResource resource = createSecureClientResource(url);
//        //resource.addQueryParameter("uid", "sample.zip");
//        Form form = new Form();
//        //<parameters>
//        //<parameter id="max.create.kpi" label="Max Create KPI" type="int" value="10"/>
//        //<parameter id="max.run.kpi" label="Max Run KPI" type="int" value="8"/>
//        //</parameters>
//		form.add("info", "<parameters>"
//        +"<parameter id=\"max.create.kpi\" label=\"Max Create KPI\" type=\"int\" value=\"100\"/>"
//        +"<parameter id=\"max.run.kpi\" label=\"Max Run KPI\" type=\"int\" value=\"80\"/>"
//        +"<parameter id=\"string\" type=\"string\" label=\"String 字符串\" value=\"中文local~!@#$%^*()_+-={}|[]\\:;?,./%\"/>"
//        +"</parameters>");
//
//		Representation rep = resource.post(form.getWebRepresentation());
//        String text = rep.getText();
//        System.out.println("testParameter");
//        System.out.println(text);
//        assertNotNull(text);
//    }
    
	public void testStartPdModel() throws IOException{
		String appID = "/pd";
        String url = APP_URL_PREFIX + appID+"?op=start.model";
        ClientResource resource = createSecureClientResource(url);
        Form form = new Form();
		form.add("id", "b0050898-3ddd-4b60-8351-e0ba1bf0dede");
		Representation rep = resource.post(form.getWebRepresentation());
        String text = rep.getText();
        System.out.println("testStartPdModel");
        System.out.println(text);
        assertNotNull(text);
	}
	
	
//    @Test
//    public void testStopPdModel() throws IOException{
//        
//    	String appID = "/pd";
//    	String url = APP_URL_PREFIX + appID+"?op=stop.model";
//    	ClientResource resource = createSecureClientResource(url);
//    	Form form = new Form();
//		form.add("id", "c4b160be0fa9234");
//		Representation rep = resource.post(form.getWebRepresentation());
//		String text = rep.getText();
//        System.out.println("testStopPdModel");
//        System.out.println(text);
//        assertNotNull(text);
//    }
    
    @Test
    private static String readFromFile(String fileName) {
		try {
			StringBuilder sb = new StringBuilder();
			FileInputStream fstream = new FileInputStream(fileName);
			java.io.DataInputStream is = new java.io.DataInputStream(fstream);
			java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(is));
			String strLine;
			while ((strLine = br.readLine()) != null) {
				sb.append(strLine);
			}
			is.close();
			return sb.toString();
		}
		catch (Exception e) {
			System.err.println("Error: " + e.getMessage());
		}
		return null;
	}

}
